package com.tsingteng.cosfun.ui.cosfun.adapter;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.pili.pldroid.player.widget.PLVideoTextureView;
import com.pili.pldroid.player.widget.PLVideoView;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.HomeBean;
import com.tsingteng.cosfun.bean.OpusBean;
import com.tsingteng.cosfun.helper.DialogHelper;
import com.tsingteng.cosfun.ui.cosfun.PlayActivity;
import com.tsingteng.cosfun.ui.cosfun.cosinterface.ObserverListener;
import com.tsingteng.cosfun.ui.cosfun.cosinterface.ObserverManager;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.StringUtils;
import com.tsingteng.cosfun.widget.zan.LikeView;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class XipuVideoPlayAdapter extends RecyclerView.Adapter {
    public enum FooterState {
        Normal, Loading, TheEnd;
    }

    private final int FOOTERLAYOUT = 100;//加载更多
    public FooterHolder mFooterHolder;
    Activity mContext = null;
    List<OpusBean> videoList = null;

    private List<String> url = new ArrayList<>();
    private ArrayList<String> urlPath = new ArrayList<>();
    private final Dialog loadingDialog;
    int h = 0;
    int w = 0;
    private boolean hasPip = false;

    OnItemClickLisenter onItemClickLisenter = null;

    public XipuVideoPlayAdapter(Activity mContext, List<OpusBean> videoList, OnItemClickLisenter onItemClickLisenter) {
        this.mContext = mContext;
        this.videoList = videoList;
        loadingDialog = DialogHelper.getLoadingDialog(mContext);
        w = AppUtils.getScreenWidthAndHeight(mContext)[0];
        h = AppUtils.getScreenWidthAndHeight(mContext)[1];
        this.onItemClickLisenter = onItemClickLisenter;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == 0) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.xipu_videoplay_item_view, parent, false);
            return new XipuVideoPlayAdapter.ViewHolder(view);
        } else {//加载更多
            view = LayoutInflater.from(mContext).inflate(R.layout.listview_footer, parent, false);
            mFooterHolder = new FooterHolder(view);
            return mFooterHolder;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof XipuVideoPlayAdapter.ViewHolder) {
            ((XipuVideoPlayAdapter.ViewHolder) holder).setData(position);
        }
    }


    @Override
    public int getItemViewType(int position) {
        if (getItemCount() > 0 && position == getItemCount() - 1) {
            return FOOTERLAYOUT;
        } else {
            return 0;
        }
    }

    @Override
    public int getItemCount() {
        if (videoList == null) {
            return 0;
        }
        return videoList.size() + 1;
    }


    public boolean isHasPip() {
        return hasPip;
    }

    public void setHasPip(boolean hasPip) {
        this.hasPip = hasPip;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView video_cover;
        PLVideoTextureView videoMian;
        PLVideoTextureView videoSecond;
        ImageView img_play;
        RelativeLayout rootView;
        LikeView likeView;
        TextView tv_likeCount;
        TextView tv_comment;
        TextView tv_share;
        TextView tv_des;
        RelativeLayout rl_awkward;
        LinearLayout lin2;
        CircleImageView img1;
        CircleImageView img2;
        CircleImageView img3;

        CircleImageView user_img;
        ImageView add_img;
        CircleImageView user_join_img;
        TextView user_join_tes;
        ImageView join_img;
        RelativeLayout join_rel;

        public ViewHolder(View itemView) {
            super(itemView);
            video_cover = itemView.findViewById(R.id.xipu_item_view_video_cover);
            videoMian = itemView.findViewById(R.id.xipu_item_view_video_main);
            videoSecond = itemView.findViewById(R.id.xipu_item_view_video_second);
            img_play = itemView.findViewById(R.id.xipu_item_view_play);
            rootView = itemView.findViewById(R.id.xipu_item_view_root_view);
            likeView = itemView.findViewById(R.id.xipu_item_view_like);
            tv_likeCount = itemView.findViewById(R.id.xipu_item_view_likecount);
            tv_comment = itemView.findViewById(R.id.xipu_item_view_comment);
            tv_share = itemView.findViewById(R.id.xipu_item_view_share);
            rl_awkward = itemView.findViewById(R.id.xipu_item_view_awkward_rel);
            tv_des = itemView.findViewById(R.id.xipu_item_view_des);
            lin2 = itemView.findViewById(R.id.xipu_item_view_video_lin2);
            img1 = itemView.findViewById(R.id.xipu_item_view_awkward_img1);
            img2 = itemView.findViewById(R.id.xipu_item_view_awkward_img2);
            img3 = itemView.findViewById(R.id.xipu_item_view_awkward_img3);

            user_img = itemView.findViewById(R.id.xipu_item_view_user_img1);
            add_img = itemView.findViewById(R.id.xipu_item_view_user_guanzhu);
            user_join_img = itemView.findViewById(R.id.xipu_item_view_user_join);
            user_join_tes = itemView.findViewById(R.id.xipu_item_view_user_join_des);
            join_img = itemView.findViewById(R.id.xipu_item_view_join);
            join_rel = itemView.findViewById(R.id.xipu_item_view_user_join_rel);
        }

        public void setData(int position) {
            final OpusBean opusBean = videoList.get(position);
            videoMian.setDisplayAspectRatio(PLVideoView.ASPECT_RATIO_FIT_PARENT);
            videoSecond.setDisplayAspectRatio(PLVideoView.ASPECT_RATIO_FIT_PARENT);
            Glide.with(mContext).load(opusBean.getVideoCverUrl()).into(video_cover);
            videoMian.setCoverView(video_cover);
            videoMian.setVideoPath(opusBean.getVideoUrl());

            if (!TextUtils.isEmpty(opusBean.getCostarVideoInfoUrl())) {
                videoSecond.setVideoPath(opusBean.getCostarVideoInfoUrl());
                lin2.setVisibility(View.VISIBLE);
                setHasPip(true);
            } else {
                setHasPip(false);
                lin2.setVisibility(View.INVISIBLE);
            }


            Glide.with(mContext).load(opusBean.getProfileAvatar()).into(user_img);
            Glide.with(mContext).load(opusBean.getCostarProfileAvatar()).into(user_join_img);
            user_join_tes.setText(opusBean.getCostarTag());

            if (opusBean.getCostarProfileId() == 0) {
                join_rel.setVisibility(View.GONE);
            } else {
                join_rel.setVisibility(View.VISIBLE);
            }

            tv_likeCount.setText(StringUtils.formatZanCount(opusBean.getLikeCount()));
            tv_comment.setText(StringUtils.formatZanCount(opusBean.getCommentCount()));
            tv_share.setText(StringUtils.formatZanCount(opusBean.getTranspondCount()));
            tv_des.setText(opusBean.getVideoDesp());
            img1.setVisibility(View.INVISIBLE);
            img2.setVisibility(View.INVISIBLE);
            img3.setVisibility(View.INVISIBLE);
            if (opusBean.getVideoBattleCommentList() != null && opusBean.getVideoBattleCommentList().size() > 0) {
                int count = 0;
                if (opusBean.getVideoBattleCommentList().size() <= 3) {
                    count = opusBean.getVideoBattleCommentList().size();
                } else {
                    count = 3;
                }
                for (int i = 0; i < count; i++) {
                    if (i == 0) {
                        Glide.with(mContext).load(opusBean.getVideoBattleCommentList().get(i).getBattleProfileAvatar()).into(img1);
                        img1.setVisibility(View.VISIBLE);
                    }
                    if (i == 1) {
                        Glide.with(mContext).load(opusBean.getVideoBattleCommentList().get(i).getBattleProfileAvatar()).into(img2);
                        img2.setVisibility(View.VISIBLE);
                    }
                    if (i == 2) {
                        Glide.with(mContext).load(opusBean.getVideoBattleCommentList().get(i).getBattleProfileAvatar()).into(img3);
                        img3.setVisibility(View.VISIBLE);
                    }
                }
            }
            if (opusBean.getIsLike() == 1) {
                likeView.setChecked(true);
            }

            if (position == 0) {
                ObserverManager.getInstance().notifyObserver(ObserverListener.EVENT_CHANGE_OPUS, position, opusBean.getId());
//
//                MediaProgressController mediaController = new MediaProgressController(mContext, progress);
//                videoMian.setMediaController(mediaController);

//                videoMian.start();
//                videoMian.setLooping(true);
                if (lin2.getVisibility() == View.VISIBLE) {
//                    videoSecond.start();
//                    videoSecond.setLooping(true);
//                    videoSecond.setVolume(0, 0);
                }
                if (onItemClickLisenter != null) {
                    onItemClickLisenter.playFirst(position, videoMian, videoSecond);
                }
            }

            rl_awkward.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickLisenter != null) {
                        onItemClickLisenter.awkward(position, opusBean);
                    }
                }
            });

            likeView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!likeView.isChecked()) {
                        if (onItemClickLisenter != null) {
                            onItemClickLisenter.zan(1, position, opusBean);
                        }
                    } else {
                        if (onItemClickLisenter != null) {
                            onItemClickLisenter.zan(0, position, opusBean);
                        }
                    }

                }
            });
            tv_comment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickLisenter != null) {
                        onItemClickLisenter.comm(position, opusBean);
                    }
                }
            });
            tv_share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickLisenter != null) {
                        onItemClickLisenter.share(position, opusBean);
                    }
                }
            });
            lin2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext, "点击了画中画", Toast.LENGTH_LONG).show();
                    doOpenAnimator(lin2);
                }
            });
            join_img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickLisenter != null) {
                        onItemClickLisenter.join(position, opusBean);
                    }
                }
            });
        }
    }

    public void doOpenAnimator(LinearLayout view) {
        final Intent it = new Intent();
        ValueAnimator animator1 = ValueAnimator.ofInt(100, h);
        animator1.setDuration(300);
        final int l = h - 100;
        animator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int curValue = (int) animation.getAnimatedValue();
                int y = 0;
//                y = imgScrollY - (imgScrollY * (curValue - imgHeight) / l);
                view.getLayoutParams().height = curValue;
                view.getLayoutParams().width = curValue * w / h;
//                imageView1.setScrollY(y);
                view.requestLayout();


            }
        });
        animator1.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                it.setClass(mContext, PlayActivity.class);
                mContext.startActivity(it);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator1.start();


    }

    public interface OnItemClickLisenter {
        void zan(int type, int position, OpusBean opusBean);

        void share(int position, OpusBean opusBean);

        void join(int position, OpusBean opusBean);

        void guanzhu(int position, OpusBean opusBean);

        void comm(int position, OpusBean opusBean);

        void awkward(int position, OpusBean opusBean);

        void playFirst(int position, PLVideoTextureView p1, PLVideoTextureView p2);
    }


    public class FooterHolder extends RecyclerView.ViewHolder {
        View mLoadingViewstubstub;
        View mEndViewstub;
        public XipuVideoPlayAdapter.FooterState status = XipuVideoPlayAdapter.FooterState.Normal;

        public FooterHolder(View itemView) {
            super(itemView);
            mLoadingViewstubstub = itemView.findViewById(R.id.glistview_footer_content_rel);
            mEndViewstub = itemView.findViewById(R.id.glistview_footer_nomore_rel);
        }

        //根据传过来的status控制哪个状态可见
        public void setStatus(XipuVideoPlayAdapter.FooterState status) {
            this.status = status;
            switch (status) {
                case Normal:
                    setAllGone();
                    break;
                case Loading:
                    setAllGone();
                    mLoadingViewstubstub.setVisibility(View.VISIBLE);
                    break;
                case TheEnd:
                    setAllGone();
                    mEndViewstub.setVisibility(View.VISIBLE);
                    break;
                default:
                    break;
            }

        }

        //全部不可见
        void setAllGone() {
            if (mLoadingViewstubstub != null) {
                mLoadingViewstubstub.setVisibility(View.GONE);
            }
            if (mEndViewstub != null) {
                mEndViewstub.setVisibility(View.GONE);
            }
        }

    }
}
